package edu.ncsu.helloandroid;

import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class FGCScorecard extends FGCActivity {
	
	private FGCGameState state = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		final ScrollView scroll = (ScrollView)View.inflate(this, R.layout.scorecard_table, null);
		TableLayout table = (TableLayout)scroll.findViewById(R.id.scorecardTable);
		table.setStretchAllColumns(true);
		scroll.setFillViewport(true);
		table.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				//Finish activity on a non-scrolling touch
				finish();
				
			}
		});
		state = FGCGameState.getGameState(getTaskId());
		int[] scores = state.getScores();
		Resources res = getResources();
		int[] pars = res.getIntArray(R.array.hole_pars);
		int totalPars = 0;
		int totalScores = 0;
		LayoutInflater inflater = getLayoutInflater();
		for (int i = 0; i < FGCGameState.LAST_HOLE && i < state.getCurrentHole(); i++) {
			TableRow row = (TableRow)inflater.inflate(R.layout.scorecard_row, table, false);
			TextView number = (TextView)row.findViewById(R.id.hole_number);
			number.setText(Integer.toString(i+1));
			TextView par = (TextView)row.findViewById(R.id.hole_par);
			par.setText(Integer.toString(pars[i]));
			TextView score = (TextView)row.findViewById(R.id.hole_score);
			score.setText(Integer.toString(scores[i]));
			table.addView(row);
			View hr = new View(table.getContext());
			hr.setBackgroundResource(R.color.scorecard_horizontal);
			LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, 2);
			hr.setLayoutParams(lp);
			table.addView(hr);
			if(i == state.getCurrentHole()-1){
				row.setBackgroundResource(R.color.scorecard_highlight);
			}
			totalPars += pars[i];
			totalScores += scores[i];
			
		}
		TableRow row = (TableRow)inflater.inflate(R.layout.scorecard_total_row, table, false);
        
		TextView par = (TextView)row.findViewById(R.id.total_par);
		par.setText(Integer.toString(totalPars));
		TextView score = (TextView)row.findViewById(R.id.total_score);
		score.setText(Integer.toString(totalScores));
		table.addView(row);
		View hr = new View(table.getContext());
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, 2);
		hr.setLayoutParams(lp);
		hr.setBackgroundResource(R.color.scorecard_horizontal);
		
		setContentView(scroll);
		scroll.post(new Runnable(){
			public void run() {
				scroll.fullScroll(ScrollView.FOCUS_DOWN);
			}
		});
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		dismissOnKeyPress(keyCode,event);
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected boolean showScorecardItem() {
		return false;
	}

	public boolean onTouchEvent(MotionEvent event) {
		finish();
		return super.onTouchEvent(event);
	}
}
